<!-- 成绩标准管理-->
<template>
  <div class="courseBox">
    <div class="courseBoxTop">
      <div class="courseBoxTopSearch">
        <div class="standards">
          <span>条件名称：</span>
          <el-input
            v-model="formSearch.strategyName"
            placeholder="请输入条件名称"
          ></el-input>
        </div>

        <div class="standards">
          <span style="width: 160px">条件类型：</span>
          <el-select
            v-model="formSearch.strategyType"
            placeholder="请选择条件类型"
          >
            <el-option
              v-for="item in option"
              :key="item.typeName"
              :label="item.typeName"
              :value="item.typeName"
            ></el-option>
          </el-select>
        </div>

        <div class="standards">
          <span>状态：</span>
          <el-select v-model="formSearch.state" placeholder="请选择状态">
            <el-option label="启用" value="0"></el-option>
            <el-option label="停用" value="1"></el-option>
          </el-select>
        </div>

        <div class="searchAndReset">
          <el-button class="searchBtn" @click="search">
            <i class="el-icon-search"></i>
            <span>搜索</span>
          </el-button>
          <el-button class="resetBtn" @click="refreshInput">
            <i class="el-icon-refresh"></i>
            <span>重置</span>
          </el-button>
        </div>
      </div>
    </div>

    <div class="courseBoxBottom">
      <div class="bottomHeader">
        <span class="title">管理列表</span>
        <div class="bottomHeaderBtn">
          <el-button class="addBtn" @click="openAddDialog">
            <i class="el-icon-plus"></i>
            <span>新增</span>
          </el-button>
          <el-button class="delBtn" @click="batchDelStandard">
            <i class="el-icon-close"></i>
            <span>批量删除</span>
          </el-button>
        </div>
      </div>

      <div class="bottomMian">
        <!-- 表格 -->
        <el-table
          ref="multipleTable"
          :data="tableData"
          tooltip-effect="dark"
          style="width: 100%"
          @selection-change="handleSelectionChange"
        >
          <el-table-column type="selection" width="55"> </el-table-column>
          <el-table-column type="index" label="序号" width="55">
          </el-table-column>
          <el-table-column
            label="条件名称"
            prop="strategyName"
          ></el-table-column>
          <el-table-column prop="strategyType" label="条件类型">
          </el-table-column>
          <el-table-column prop="createTime" label="创建时间">
          </el-table-column>
          <el-table-column prop="state" label="状态">
            <template slot-scope="scope">
              <el-switch
                v-model="scope.row.state"
                active-color="#13ce66"
                inactive-color="#C2C2C2"
                @change="updateIsUsing(scope.row)"
              >
              </el-switch>
            </template>
          </el-table-column>
          <el-table-column fixed="right" label="操作">
            <template slot-scope="scope">
              <span @click="openEditDialog(scope.row)" class="icon edit"
                >编辑</span
              >
              <span @click="delStandard(scope.row)" class="icon del">删除</span>
            </template>
          </el-table-column>
        </el-table>

        <!-- 分页 -->
        <div class="pagination" v-if="tableData.length > 0">
          <el-pagination
            @current-change="handleCurrentChange"
            :current-page="queryParams.pageNum"
            :page-size="10"
            prev-text="上一页"
            next-text="下一页"
            layout=" prev, pager, next"
            :total="total"
          >
          </el-pagination>
          <div class="jumper">
            跳到第
            <input type="text" ref="currentPage" value="1" />
            页
          </div>
          <button class="submit" @click="goPage">确定</button>
        </div>
      </div>
    </div>

    <!-- 新增-->
    <div class="dialog">
      <el-dialog  :title="isEdit ? '修改' : '添加' " :visible.sync="addDialog" center>
        <span>
          <el-form ref="form" :model="form" label-width="120px">
            <el-form-item label="条件名称">
              <el-input
                v-model="form.strategyName"
                placeholder="请输入条件名称"
              ></el-input>
            </el-form-item>
            <el-form-item label="条件类型">
              <el-select
                v-model="form.strategyType"
                placeholder="请选择条件类型"
              >
                <el-option
                  v-for="item in option"
                  :key="item.typeName"
                  :label="item.typeName"
                  :value="item.typeName"
                ></el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="毕业&结业条件">
              <el-table
                :data="form.conditions"
                height="230"
                border
                style="width: 100%"
              >
                <el-table-column label="学科">
                  <template slot-scope="scope">
                    <el-select
                      style="width:60px"
                      type="text"
                      v-model="scope.row.condition"
                    >
                      <el-option
                        v-for="(item, index) in typelist"
                        :key="index"
                        :label="item.typeName"
                        :value="item.typeId"
                      >
                      </el-option>
                    </el-select>
                    <!-- <el-input
                      type="text"
                      v-model="scope.row.condition"
                    ></el-input> -->
                  </template>
                </el-table-column>
                <el-table-column label="学分">
                  <template slot-scope="scope">
                    <el-input
                      type="text"
                      v-model="scope.row.content"
                    ></el-input>
                  </template>
                </el-table-column>
                <el-table-column label="删除" width="50">
                  <template slot-scope="scope">
                    <span
                      @click="del(scope.row)"
                      class="icon iconfont icon-shanchu"
                    ></span>
                  </template>
                </el-table-column>
              </el-table>
              <el-button
                icon="el-icon-plus"
                circle
                @click="tianjia"
                class="btn"
              ></el-button>
            </el-form-item>
          </el-form>
        </span>
        <span slot="footer" class="dialog-footer">
          <el-button @click="closeAddDialog">取 消</el-button>
          <el-button type="primary" @click="addSubmit">确 定</el-button>
        </span>
      </el-dialog>
    </div>
  </div>
</template>

<script>
import "@/styles/picture.scss";
import { _debounce } from "@/utils/debounce";
import { getGradeListApi } from "@/api/basics/teachingPlan";
import { getNationApi } from "@/api/basics/classroom";
import {
  addGraduationreportApi,
  delGraduationreportApi,
  editGraduationreportApi,
  getGraduationreportListApi,
} from "@/api/graduation/graduationreport";
export default {
  data() {
    return {
      //搜索框数据
      formSearch: {
        strategyName: "",
        state: "",
        strategyType: "",
      },
      option: [],
      tableData: [], //表格
      //分页
      queryParams: {
        pageNum: 1,
        pageSize: 10,
      },
      total: 0, //总条数
      addDialog: false, //添加弹出框
      editDialog: false, //修改弹框
      isEdit: false,
      typelist: "",
      //添加
      form: {
        strategyName: "",
        conditions: [],
        strategyType: "",
        id: "",
      },
      multipleSelection: [],
    };
  },

  mounted() {
    // 获取条件列表
    this.getGraduationreportList({
      ...this.queryParams,
    });
    this.getList();
    // 获取 考试类型 列表
    this.getGradeList();
  },

  methods: {
    del(data) {
      this.form.conditions.forEach((item, index) => {
        if (item.ind === data.ind) {
          this.form.conditions.splice(index, 1);
        }
      });
    },

    // 获取条件列表
    getGraduationreportList(data) {
      getGraduationreportListApi({
        ...data,
      }).then((res) => {
        if (res.code === 200) {
          this.total = res.total;
          this.tableData = res.rows.map((item) => {
            return {
              ...item,
              conditions:
                item.conditions != "null"
                  ? JSON.parse(item.conditions)
                  : item.conditions == "null"
                  ? []
                  : [],
              state: item.state == "0" ? true : false,
            };
          });
        }
      });
    },

    // 获取 条件类型 列表
    async getGradeList() {
      let res = await getGradeListApi({ parentId: 206 });
      this.option = res.rows;
    },
    async getList() {
      let res = await getNationApi({ parentId: 130 });
      this.typelist = res.rows;
    },
    //搜索
    search: _debounce(function () {
      // 获取条件列表
      this.getGraduationreportList({
        ...this.queryParams,
        ...this.formSearch,
      });
    }, 500),

    // 重置
    refreshInput: _debounce(function () {
      this.formSearch = {
        strategyName: "",
        state: "",
        strategyType: "",
      };
      // 获取条件列表
      this.getGraduationreportList({
        ...this.queryParams,
      });
    }, 500),

    // 点击新增
    openAddDialog() {
      this.form = {
        strategyName: "",
        conditions: [],
        strategyType: "",
        id: "",
      };
      this.isEdit = false;
      this.addDialog = true;
      this.form.conditions = [];
    },

    // 取消新增
    closeAddDialog() {
      this.addDialog = false;
    },

    // 点击编辑
    openEditDialog(data) {
      let { strategyName, conditions, strategyType, id } = data;
      this.form = {
        strategyName,
        conditions,
        strategyType,
        id,
      };
      this.isEdit = true;
      this.addDialog = true;
    },

    //新增添加表格数据
    tianjia: _debounce(function () {
      this.form.conditions.push({
        condition: "",
        content: "",
        ind: this.form.conditions.length,
      });
    }, 200),

    //添加
    addSubmit: _debounce(function () {
      let { strategyName, strategyType } = this.form;
      if (strategyName && strategyType) {
        if (this.isEdit) {
          this.$message("修改中~");
          editGraduationreportApi({ ...this.form }).then((res) => {
            if (res.code === 200) {
              this.addDialog = false;
              this.getGraduationreportList({
                ...this.queryParams,
              });
              this.$message.success("修改成功!");
            } else {
              this.$message.error("修改失败!");
            }
          });
        } else {
          this.$message("添加中~");
          addGraduationreportApi({ ...this.form, state: "0" }).then((res) => {
            if (res.code === 200) {
              this.addDialog = false;
              this.getGraduationreportList({
                ...this.queryParams,
              });
              this.$message.success("添加成功!");
            } else {
              this.$message.error("添加失败!");
            }
          });
        }
      } else if (!strategyName) {
        this.$message.error("请输入条件名称!");
      } else if (!strategyType) {
        this.$message.error("请选择条件类型!");
      }
    }, 200),

    // 删除
    delStandard: _debounce(function (data) {
      let ids = data.id + "";
      this.$confirm("此操作将删除已选择的条件, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          delGraduationreportApi({ ids }).then((res) => {
            if (res.code === 200) {
              this.$message.success("删除成功!");
              // 获取条件列表
              this.getGraduationreportList({
                ...this.queryParams,
              });
            } else {
              this.$message.error("删除失败!");
            }
          });
        })
        .catch(() => {});
    }, 200),

    // 批量删除
    batchDelStandard: _debounce(function () {
      if (this.multipleSelection.length) {
        let data = [...this.multipleSelection].map((item) => {
          return "" + item.id;
        });
        this.$confirm("此操作将删除已选择的条件, 是否继续?", "提示", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        })
          .then(() => {
            delGraduationreportApi({ ids: data.join(",") }).then((res) => {
              if (res.code === 200) {
                this.$message.success("删除成功!");
                this.getGraduationreportList({
                  ...this.queryParams,
                });
              } else {
                this.$message.error("删除失败!");
              }
            });
          })
          .catch(() => {});
      } else {
        this.$message.error("请选择要删除的条件！");
      }
    }, 200),

    // 修改启用状态
    updateIsUsing: _debounce(function (data) {
      let { id, state } = data;
      this.$message("修改中~");
      editGraduationreportApi({
        id,
        state: state ? "0" : "1",
      }).then((res) => {
        if (res.code === 200) {
          this.$message.success("修改成功!");
        } else {
          this.$message.error("修改失败!");
        }
        // 获取条件列表
        this.getGraduationreportList({
          ...this.queryParams,
        });
      });
    }, 200),

    // 已选中
    handleSelectionChange(val) {
      this.multipleSelection = val;
    },

    // 设置当前页码
    handleCurrentChange(val) {
      this.queryParams.pageNum = val;
      this.getGraduationreportList({
        ...this.queryParams,
      });
    },

    // 跳转指定页码
    goPage() {
      let { value } = this.$refs.currentPage;
      this.queryParams.pageNum = Number(value);
      this.getGraduationreportList({
        ...this.queryParams,
      });
    },
  },
};
</script>

<style lang="scss" scoped>
.courseBox .courseBoxTop,
.courseBox .courseBoxBottom {
  min-width: calc(130vh);
}

.courseBox .courseBoxTop .courseBoxTopSearch .pictureVersion {
  margin-left: 25px;
}

.courseBoxTopSearch {
  ::v-deep .el-input__inner {
    height: 36px;
    line-height: 36px;
  }

  .standards {
    display: flex;
    align-items: center;
    margin-bottom: 20px;

    span {
      width: 130px;
      font-size: 14px;
      text-align: right;
    }
  }
}

::v-deep .el-icon-date {
  line-height: 36px;
}
.del {
  margin-left: 10px;
  // font-weight: bold;
  color: #1990ff;
  cursor: pointer;
}
.edit {
  // font-weight: bold;
  margin-right: 10px;
  color: #1990ff;
  cursor: pointer;
}
.dialog {
  .block {
    width: 280px;
    height: 30px;
    border: solid 1px #dcdfe6;
    border-radius: 4px;
    display: flex;
    align-items: center;
    justify-content: space-between;
    box-sizing: border-box;
    background-color: #fff;

    ::v-deep .el-input__inner {
      width: 240px;
      height: 28px;
      border: none;
    }

    ::v-deep .el-icon-arrow-down {
      color: #c2c2c2;
      padding: 0 10px 0 0;
    }

    ::v-deep .el-icon-arrow-up {
      color: #c2c2c2;
      padding: 0 10px 0 0;
    }

    ::v-deep .el-date-picker.has-sidebar {
      left: 460px !important;
      background-color: #4895db;
    }
  }

  ::v-deep .el-dialog {
    max-width: 750px;
    min-width: 500px;
    border-radius: 10px;
    background-color: #f9f9f9;
  }

  ::v-deep .el-dialog__header {
    height: 48px;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 18px;
    background-color: #4895db;
    padding: 0;
    border-radius: 10px 10px 0 0;

    span {
      font-size: 16px;
      color: #fff;
    }
  }

  ::v-deep .el-select > .el-input {
    display: block;
    width: 300px;
  }

  ::v-deep .el-form-item__label {
    line-height: 32px;
  }

  ::v-deep .el-input__inner {
    height: 30px;
    line-height: 30px;
  }

  ::v-deep .el-dialog__headerbtn {
    top: 15px;
  }

  ::v-deep .el-dialog__headerbtn .el-dialog__close {
    color: #fff;
    font-size: 18px;
  }

  ::v-deep .el-dialog--center .el-dialog__body {
    text-align: initial;
    display: flex;
    justify-content: center;
  }

  ::v-deep .el-input__icon {
    line-height: 30px;
  }

  ::v-deep .el-form-item__content {
    width: 300px;
    line-height: 30px;
  }

  ::v-deep .el-form-item {
    margin-bottom: 15px;
  }

  ::v-deep .el-dialog__footer {
    padding: 10px 20px 40px;
  }

  ::v-deep .el-button--default {
    width: 130px;
    border-radius: 20px;
    background-color: #f0f0f0;
    color: #aaaaaa;
    border: none;
  }

  ::v-deep .el-button + .el-button {
    width: 130px;
    border-radius: 20px;
    margin-left: 50px;
    background-color: #4895db;
    border-color: #4895db;
  }
}

.courseName,
.profession {
  ::v-deep .el-input__inner {
    width: 200px;
  }

  ::v-deep .el-date-editor.el-input {
    width: 200px;
  }
}

.pictureVersion {
  ::v-deep .el-input__inner {
    width: 140px;
  }

  ::v-deep .el-date-editor.el-input {
    width: 140px;
  }
}

.btn {
  width: 30px !important;
  height: 30px !important;
  float: right;
  margin-top: 10px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  background-color: #fff !important;
  border: solid 1px #aaaaaa !important;

  .el-icon-plus {
    font-weight: bolder;
    color: #aaaaaa;
  }
}
</style>